﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using NTTDATA.BEENChina.ResultCertification.Client.Entry.Model;
using NTTDATA.BEENChina.ResultCertification.Client.Entry.ServiceEntry;

namespace NTTDATA.BEENChina.ResultCertification.Client.Entry.Logic
{
    public delegate void ChangeEventHandler();//翻页刷新
    public delegate void CheckLogingEventHandler(Judge judge);//检测是否登入
    public class EntryJob
    {
        public event ChangeEventHandler Page_change;
        public event CheckLogingEventHandler Page_Check;

       public void CheckPage(Judge judge)
        {
            if (null != this.Page_Check)
            {
                Page_Check(judge);
            }
        } 

        public int TimeOut { get; set; }


        public void UpdatePage()
        {
            if (null != this.Page_change)
            {
                Page_change();
            }
        }

        /// <summary>
        /// 当前显示的Index值
        /// </summary>
        private int _Index = 0;

        public int Index
        {
            get
            {
                return _Index;
            }
            set
            {
                _Index = value;
            }
        }
        public DateTime ImageStartTime { get; set; }

        public void CalculationPictureTime(int index)
        {
            DateTime end = DateTime.Now;//结束时间
            TimeSpan ts = end - this.ImageStartTime;
            int iC = (int)ts.TotalMilliseconds;
            JobItem.ChildImage[index].VerfityConfirmResult.VerfityTime += iC;
 
            this.ImageStartTime = DateTime.Now;//另一张图片的开始时间

            }

        /// <summary>
        /// 画像个数
        /// </summary>
        public int ItemCount = 0;

        #region 翻页功能
        /// <summary>
        /// 向前翻页
        /// </summary>
        public void BackPage()
        {
            CalculationPictureTime(_Index);
            _Index--;
            if (Index < 0)
            {
                _Index = 0;
                return;
            }
            UpdatePage();
        }

        /// <summary>
        /// 向后翻页
        /// </summary>
        public void NextPage()
        {
            if (_Index != this.JobItem.ChildImage.Count - 1)
            {
                CalculationPictureTime(_Index);
            }
            _Index++;
            if (Index >= this.JobItem.ChildImage.Count)
            {
                _Index = this.JobItem.ChildImage.Count - 1;
                return;
            }
            UpdatePage();
        }

        /// <summary>
        /// 翻到第一页
        /// </summary>
        public void FirstPage()
        {
            CalculationPictureTime(_Index);
            _Index = 0;
            UpdatePage();
        }

        /// <summary>
        /// 翻到最后一页
        /// </summary>
        public void LastPage()
        {
            CalculationPictureTime(_Index);
            _Index = JobItem.ChildImage.Count - 1;
            UpdatePage();
        }
        #endregion

         

        public VerfityJobItem JobItem { get; set; }

        /// <summary>
        /// 返回登录数
        /// </summary>
        /// <returns></returns>
        public int GetLoginCount()
        {
            int Result = 0;
            
            return Result;
        }

        private int NotEntry()
        {
            int Result = -1;
            for (int i = 0; i < this.JobItem.ChildImage.Count; i++)
            {
                if (String.IsNullOrEmpty(this.JobItem.ChildImage[i].VerfityConfirmResult.Result) || this.JobItem.ChildImage[i].VerfityConfirmResult.Result =="0")
                {
                    Result = i;
                    break;
                }
            }
          
            return Result;
        } 

        /// <summary>
        /// 未入力查询
        /// </summary>
        public bool SearchNoEntry()
        {
            int iT = NotEntry();
            if (iT >= 0)
            {
                this._Index = iT;
                UpdatePage();
                return true;
            }
            return false;
        }
        //Job提交
        public int JobUpLoad()
        {
            
            CalculationPictureTime(_Index);

            //留存画像
            //如果需要本地留存图像
           // SnapImage();

           return  new JobListHelper().UpLoadJob(LoginHelper.GetInstance().UserID, this.JobItem);
           
        }


        public int CancelJob()
        {
            return LoginHelper.GetInstance().CancelJob();
        }

        /// <summary>
        ///登录操作 
        /// </summary>
        public void UpdateLogin(int judgeAnswer, string comment, int missWord,EnumMissType missType)
        {
            JobItem.ChildImage[_Index].VerfityConfirmResult.Result = judgeAnswer.ToString(CultureInfo.InvariantCulture)  ;
            JobItem.ChildImage[_Index].VerfityConfirmResult.MissWord = missWord;
            JobItem.ChildImage[_Index].VerfityConfirmResult.VerfityComment = comment;

            JobItem.ChildImage[_Index].VerfityConfirmResult.MissType = missType;

            NextPage();
        }

        
        #region 分钟转化成标准小时
        /// <summary>
        /// 将分钟转化成 小时：分钟：秒
        /// </summary>
        /// <param name="intParam"> 分钟数值</param>
        /// <returns>返回为转换的值</returns>
        public int[] intGetTestTime(int Timeout)
        {
            int[] intVer = new int[3];
            TimeSpan objTimeSpan;//间隔小时
            int intSapn = 0, intTemp = 0;
            if (Timeout > 1440)
            {
                intSapn = Timeout / 1440;
                intTemp = intSapn;
                intSapn = Timeout - intSapn * 1440;
                objTimeSpan = new TimeSpan(0, 0, intSapn, 0);
                intVer[0] = objTimeSpan.Hours;
                intVer[1] = objTimeSpan.Minutes;
                intVer[2] = objTimeSpan.Seconds;
            }
            else if (Timeout == 0)
            {
                intVer[0] = 0;
                intVer[1] = 0;
                intVer[2] = 59;
            }
            else
            {
                objTimeSpan = new TimeSpan(0, 0, Timeout, 0);
                intVer[0] = objTimeSpan.Hours;
                intVer[1] = objTimeSpan.Minutes;
                intVer[2] = objTimeSpan.Seconds;
            }
            return intVer;
        }
        #endregion
        /// <summary>
        /// 显示时间
        /// </summary>
        /// <param name="intHours"></param>
        /// <param name="intMinutes"></param>
        /// <param name="intSeconds"></param>
        /// <returns></returns>
        public string strShowTime(int intHours, int intMinutes, int intSeconds)
        {
            return String.Format("{0:D2}", intHours) + "時間" + String.Format("{0:D2}", intMinutes) + "分" + String.Format("{0:D2}", intSeconds) + "秒";
        }


        public bool DowindingImage(string Path, string UserID)
        {
/*            int iCount = 0;
            FileStream writefs;
            try
            {
                if (null != JobPage)
                {
                    iCount = JobPage.Count;
                }
                Path = Path + "\\";
                foreach (Page page in this.JobPage)
                {
                    if (null != page.Image.Image)
                    {
                        if (page.Image.Image.Length > 0)
                        {
                            writefs = new FileInfo(Path + page.Image.ImageName).Create();
                            writefs.Write(page.Image.Image, 0, page.Image.Image.Length);
                            writefs.Close();
                            writefs.Dispose();
                            writefs = null;
                            if (File.Exists(Application.StartupPath + "\\" + Path + "\\" + page.Image.ImageName))
                            {
                                page.Image.Image = null;
                            }
                            else
                            {
                                this.CancelJob(page.SEQNo, UserID);

                                //todo writeimage error to wcf
                                //WCFHelper.GetInstance().WriteImageErrLog(UserID, page.Image.ImageName, page.Image.ImagePath, Application.StartupPath + "\\" + Path + "\\" + page.Image.ImageName);
                                List<Page> lists = new List<Page>();
                                lists.AddRange(this.JobPage);
                                lists.Remove(page);
                                this.JobPage = lists;
                            }
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Code 1017: " + " 異常を発生 " + ex.Message, "メッセージ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            */

            return true;
        }


        public void DeleteImage()
        {
            
        }

        public static string GetCommnetName(string commentId)
        {
            
            /*10 その他
            11 照合アンマッチ
            12 文字記載
            14 記載異常
            15 画像異常
            20 判読不能
            22 要Fチェック
            30 未記載
            40 フリガナ未記載
            45 読下しフリガナ
            50 記載内容　①
            60 類字対応
            70 同上記載
            72 ncomuse
            75 依頼主同上
            80 複数記載
            85 置き換え付箋
            88 Eク作成用②
            90 判断入力
            91 規定外付箋
            92 件数超過付箋
            94 記載の「？」
            95 訂正印有
            96 取消線有
            97 正解
            98 不正解
            99 桁オーバー*/
            string commentName = "";

            switch (commentId)
            {
                case "10": 
                    commentName = "その他";
                    break;
                case "11": 
                    commentName = "照合アンマッチ";
                    break;
                case "12": 
                    commentName = "文字記載";
                    break;
                case "14": 
                    commentName = "記載異常";
                    break;
                case "15": 
                    commentName = "画像異常";
                    break;
                case "20": 
                    commentName = "判読不能";
                    break;
                case "22": 
                    commentName = "要Fチェック";
                    break;
                case "30": 
                    commentName = "未記載";
                    break;
                case "40": 
                    commentName = "フリガナ未記載";
                    break;
                case "45": 
                    commentName = "読下しフリガナ";
                    break;
                case "50": 
                    commentName = "記載内容　①";
                    break;
                case "60": 
                    commentName = "類字対応";
                    break;
                case "70": 
                    commentName = "同上記載";
                    break;
                case "72": 
                    commentName = "ncomuse";
                    break;
                case "75": 
                    commentName = "依頼主同上";
                    break;
                case "80": 
                    commentName = "複数記載";
                    break;
                case "85": 
                    commentName = "置き換え付箋";
                    break;
                case "88": 
                    commentName = "Eク作成用②";
                    break;
                case "90": 
                    commentName = "判断入力";
                    break;
                case "91": 
                    commentName = "規定外付箋";
                    break;
                case "92": 
                    commentName = "件数超過付箋";
                    break;
                case "94": 
                    commentName = "記載の「？」";
                    break;
                case "95": 
                    commentName = "訂正印有";
                    break;
                case "96":
                    commentName = "取消線有";
                    break;
                case "97": 
                    commentName = "正解";
                    break;
                case "98": 
                    commentName = "不正解";
                    break;
                case "99": 
                    commentName = "桁オーバー";
                    break;
                
                //E result
                case "1":
                    commentName = "正解";
                    break;

                case "2":
                    commentName = "修正";
                    break;
            }

            return commentName;
        }

        //public static List<List<string>>  GetSplitResult(CategoryConfig categoryConfig, string entryResult)
        //{
        //    List<List<string>> splitResult = null;
        //    using ( ServiceEntry.ServiceEntryClient client = new ServiceEntryClient( ))
        //    {
        //        splitResult = client.GetSplitResult(categoryConfig, entryResult);
        //    }
        //    return splitResult;
        //}


        /// <summary>
        /// 留存图片
        /// </summary>
        private void SnapImage()
        {
            foreach (var item in JobItem.ChildImage)
            {
                if (item.VerfityConfirmResult.Result.Equals(
                    Judge.F2.ToString()))
                {

                    string fileName = item.ImageFullPath;
                    string imageid = item.ImageId;
                    string targetFile = System.IO.Path.Combine(Environment.CurrentDirectory, @"SnapImage\" + imageid);

                    File.Copy(fileName, targetFile, true);
                }
            }
            

        }
    }
}
